package com.contory.ssn_lgnd.framework.gl;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.contory.ssn_lgnd.framework.math.Rectangle;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class Camera2D {
    final Vector2 position;
    final float frustumWidth;
    final float frustumHeight;
    int frameBufWidth;
    int frameBufHeight;
    final float[] mProjMatrix = new float[16];
    Rectangle boundRect;
    
    public Camera2D(int frameBufWidth, int frameBufHeight, float frustumWidth, float frustumHeight) {
    	this.frameBufWidth = frameBufWidth;
    	this.frameBufHeight = frameBufHeight;
    	this.frustumWidth = frustumWidth;
        this.frustumHeight = frustumHeight;
        this.position = new Vector2(frustumWidth / 2, frustumHeight / 2);
        boundRect = new Rectangle(position.x - frustumWidth/ 2, position.y - frustumHeight/ 2, frustumWidth, frustumHeight);
    }
    
    public void setPosit(float x, float y){
    	position.x = x;
    	position.y = y;
    	boundRect.setLowerLeft(x - frustumWidth/ 2, y - frustumHeight/ 2);
    }
    
    public void setPosX(float x){
    	position.x = x;
    	boundRect.lowerLeft.x = x - frustumWidth/ 2;
    }
    
    public void setPosY(float y){
    	position.y = y;
    	boundRect.lowerLeft.y = y - frustumHeight/ 2;
    }
    
    public Rectangle getBoundRect(){
    	return boundRect;
    }
    
    public Vector2 getPosition(){
    	return position;
    }
    
    public float getFrustumWidth(){
    	return frustumWidth;
    }
    
    public float getFrustumHeight(){
    	return frustumHeight;
    }
    
    public void setViewPort(int viewPortWidth, int viewPortHeight){
    	GLES20.glViewport(0, 0, viewPortWidth, viewPortHeight);
    }
  
    public void setViewPort(FrameBufferObject fb){
    	GLES20.glViewport(0, 0, fb.width, fb.height);
    }
    
    public void setMatrices(ESShader shader, String matrixName) {
    	Matrix.orthoM(mProjMatrix, 0,
    			position.x - frustumWidth / 2, 
    			position.x + frustumWidth / 2, 
    			position.y - frustumHeight / 2, 
    			position.y + frustumHeight / 2, 
    			1, -1);
    	GLES20.glUniformMatrix4fv(shader.getUniform(matrixName), 1, false, mProjMatrix, 0);
    }
    
    public void setOrinMatrices(ESShader shader, String matrixName){
    	Matrix.orthoM(mProjMatrix, 0,
    			0, 
    			frustumWidth, 
    			0, 
    			frustumHeight, 
    			1, -1);
    	GLES20.glUniformMatrix4fv(shader.getUniform(matrixName), 1, false, mProjMatrix, 0);
    }
    
    public void touchToWorld(Vector2 touch) {
        touch.x = (touch.x / (float) frameBufWidth) * frustumWidth;
        touch.y = (1 - touch.y / (float) frameBufHeight) * frustumHeight;
        touch.add(position).sub(frustumWidth / 2, frustumHeight / 2);
    }
    
    public void touchToOrinWorld(Vector2 touch){
    	touch.x = (touch.x / (float) frameBufWidth) * frustumWidth;
        touch.y = (1 - touch.y / (float) frameBufHeight) * frustumHeight;
        //touch.sub(frustumWidth / 2, frustumHeight / 2);
    }
}
